/**
 * express 使用cookie
 * cookie 加密
 *  1、直接在cookie保存加密
 *  2、使用参数设置signed设置为true
 *      app.use(cookieParser("signed")); 设置加密字符串
 *        res.cookie("name",'zhangsan',{maxAge: 900000, signed: true}); //设置使用加密cookie
 *        console.log(req.signedCookies); //读取加密的cookie
 *
 *
 */
const express = require('express');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');

const app = express();

app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());
//app.use(cookieParser()); //设置cookie
app.use(cookieParser("signed")); //使用signed加密cookies

app.get('/',function (req,res) {
    console.log(req.cookies);
    res.send('hello express');
});

app.get('/set',function (req,res) {
    /**
     * 参数1 key
     * 参数2 value
     * 参数3 cookie的设置信息
     * signed 设置加密  注意需要在app.use中使用cookieParser的时候增加密码
     */
    res.cookie("name",'zhangsan',{maxAge: 900000, signed: true});
    res.send('set');
});

app.get('/get',function (req,res) {
    //获取加密的cookie
    console.log(req.signedCookies);
    res.send('get cookie name:'+req.signedCookies.name);
});

app.get('/news',function (req,res) {
    //读取cookie
    console.log(req.signedCookies);
    res.send('get cookie name:'+req.signedCookies.name);
});

app.listen(3001,'localhost');